package FilePackage;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

import ObjectPackage.ObjectClass;
import ObjectPackage.ObjectClass.Special;


public class Report 
{
	private ArrayList<String> report =new ArrayList<String>();
	FileClass file = new FileClass();
	private ArrayList<ObjectClass> arrObject =new ArrayList<ObjectClass>();
	public ArrayList<String> getReport(){
         return report;
	}


	/**
	 * Choose report and create its.
     *@Parameter int
     *@Parameter String
     */
	public void CreateReport(int item, String aFileName) {
		file.clear();
		file.read(aFileName);
		arrObject.clear();
		switch(item){
        case 2:
        	
        	Special first,last = null;
        	for (ObjectClass it :  sort(file.getArr(),item))
            {
            	    it.fillObject(it);
            	    first = it.getSpecial();
            	    if(it.getSpecial()!= last)
            	    {
            	    	 last = first;
               	    	 System.out.println(it.getSpecial());
            	    }else if(last==null)
            	    {
            	    	System.out.println(it.getSpecial());
            	    }
            	    
            	    arrObject.add(it);
                    System.out.println("\t" + it.getSurname() + "\t" + it.getDTime());

                    	
            }
        	
        	break;
        case 3:
        	
        	for (ObjectClass it : sort(file.getArr(),item))
            {
        		arrObject.add(it);
        		 System.out.println("\t" + it.getSurname() + "\t" + it.getDTime());
            }
        	
        	break;	
        case 4:
        	for (ObjectClass it : sort(file.getArr(),item))
            {
        		arrObject.add(it);
        		 System.out.println("\t" + it.getSurname() + "\t" + it.getDTime()+ "\t" + it.getDate());
            }
        	break;
        case 5:
        	inputDate();
        	break;
        case 6:
        	inputName();
        	break;
        }
		
	}
	
	@SuppressWarnings("resource")
	private void inputName() {
		System.out.println("Input Name Master: ");
		Scanner c = new Scanner(System.in);
		Boolean bool=false;
		String MasterName = c.nextLine();
		for(ObjectClass obj : file.getArr())
		{
			obj.fillObject(obj);
			if(MasterName.equals(obj.getMasterName()))
			{
				bool=true;
				 System.out.println("\t" + obj.getSurname()+ "\t" + obj.getName() + "\t" + obj.getDTime()+ "\t" + obj.getDate());
			}
		}
		if(!bool)
		{
			 System.out.println("Records to This Master not found!");
		}
		
	}


	@SuppressWarnings("resource")
	private void inputDate() {
		System.out.println("Input adate to format yyyy.mm.dd:");
		Scanner c = new Scanner(System.in);
		String str = c.nextLine();
		searchDate(str);
	}

	/**
	 * Find Date
     *@Parameter String
     */
	@SuppressWarnings({  "unused"})
	private void searchDate(String s) {
		String dat = s;
		Boolean bool=false;
		for(ObjectClass obj : file.getArr())
		{
			obj.fillObject(obj);
			if(s.equals(obj.getDate()))
			{
				bool=true;
				 System.out.println("\t" + obj.getSurname() + "\t" + obj.getDTime());
			}
		}
		if(!bool)
		{
			 System.out.println("Records with this date not found or not correct Date!");
		}
	}

	/**
	 * Choose sort and sorting.
     *@Parameter int
     *@Parameter ArrayList<ObjectClass>
     *@Return ArrayList<ObjectClass> 
     */

	public ArrayList<ObjectClass> sort(ArrayList<ObjectClass> array,final int item){
        
        if(item == 2|| item ==3 || item ==4)
        {
                Collections.sort(array,new Comparator<ObjectClass>(){
                        @Override
                        public int compare(ObjectClass o1, ObjectClass o2){
                                switch(item){
                                        case 2:
                                                return o1.getSpecial().toString().compareTo(o2.getSpecial().toString());
                                        case 3:
                                                return o1.getSurname().compareTo(o2.getSurname());
                                        case 4:
                                                return o1.getDate().compareTo(o2.getDate());
                                }
								return 0;                                              
                        }
                });
        }
        return array;
}
	
	public void Save(){      
        try{
 
       	 FileWriter out =new FileWriter(new File("src/storage.txt"),false);
       	 try{
       		 for(ObjectClass object:  arrObject)
       		 {
       	 	    out.write(object.ToString());
                out.write(System.getProperty("line.separator"));
       		 }
       	 }finally{
                out.close();
       	 }
        }catch(IOException e){
                throw new RuntimeException(e);
        }
}
	
}
